//
//  KZHomeBgView.m
//  听说弱点强化训练
//
//  Created by 朱凯 on 2018/11/6.
//  Copyright © 2018年 盛超. All rights reserved.
//

#import "KZHomeBgView.h"
#import "IPhoneType.h"

@implementation KZHomeBgView
{
    NSTimer *_timer;
}
- (instancetype)init {
    if (self = [super initWithFrame:UIScreen.mainScreen.bounds]) {
    
        [self setupUI];
        [self startRain];
    }
    return self;
}
- (void)setupUI {

    CAReplicatorLayer *repLayer = [CAReplicatorLayer layer];
    repLayer.backgroundColor = [UIColor clearColor].CGColor;
    repLayer.bounds = CGRectMake(0, 0, self.width, self.width);
    repLayer.position = CGPointMake(self.width / 2, self.width / 2);
    [self.layer addSublayer:repLayer];
    BOOL isiPhoneX = [IPhoneType judge_iPhoneX];
    if (isiPhoneX) {
         repLayer.position = CGPointMake(self.width / 2, 50+self.width / 2);
    }
    
    CAShapeLayer *itemLayer = [CAShapeLayer layer];
    
    itemLayer.frame = repLayer.bounds;
    itemLayer.opacity = 0.1;
    itemLayer.lineWidth = 20;
    itemLayer.strokeColor = UIColor.whiteColor.CGColor;
    itemLayer.fillColor = UIColor.clearColor.CGColor;
    itemLayer.path = [self startPath].CGPath;

    [repLayer addSublayer:itemLayer];
    
    repLayer.instanceCount = 3;
    
    // 动画
    CABasicAnimation *ani = [CABasicAnimation animationWithKeyPath:@"transform"];
    ani.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    ani.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(3.3, 3.3, 1)];
    CABasicAnimation *ani2 = [CABasicAnimation animationWithKeyPath:@"opacity"];
    ani2.toValue = @(0);
    CAAnimationGroup *g = [CAAnimationGroup animation];
    g.duration = 3;
    g.repeatCount = HUGE;
    g.animations = @[ani, ani2];
    repLayer.instanceDelay = g.duration / repLayer.instanceCount;
    [itemLayer addAnimation:g forKey:@"ani"];
}

- (UIBezierPath *)startPath {
    
    CGFloat margin = self.width / 2 * 0.7;
    CGFloat angle = M_PI * 2 / 5;
    CGFloat radius = self.width / 2 - margin;
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(self.width / 2, margin)];
    [path addLineToPoint:CGPointMake(self.width / 2 + radius * sin(angle), self.width / 2 - radius * cos(angle))];
    [path addLineToPoint:CGPointMake(self.width / 2 + radius * sin(angle/2), self.width / 2 + radius * cos(angle/2))];
    [path addLineToPoint:CGPointMake(self.width / 2 - radius * sin(angle/2), self.width / 2 + radius * cos(angle/2))];
    [path addLineToPoint:CGPointMake(self.width / 2 - radius * sin(angle), self.width / 2 - radius * cos(angle))];
    [path closePath];
    
    return path;
}


- (CAAnimationGroup *)ani {
    // 动画
    CABasicAnimation *ani = [CABasicAnimation animationWithKeyPath:@"transform"];
    ani.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    ani.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(3.3, 3.3, 1)];
    CABasicAnimation *ani2 = [CABasicAnimation animationWithKeyPath:@"opacity"];
    ani2.toValue = @(0);
    CAAnimationGroup *g = [CAAnimationGroup animation];
    g.duration = 3;
    g.repeatCount = HUGE;
    g.animations = @[ani, ani2];
    return g;
}

- (void)rain {
    
    UIImageView *meteorView = [UIImageView new];
    meteorView.bounds = CGRectMake(0, 0, 40, 40);
   // meteorView.backgroundColor = UIColor.whiteColor;
    meteorView.image = [UIImage imageNamed:@"LS-流星"];
    [self addSubview:meteorView];
    
    NSInteger random = arc4random()%2;
    if (random==0) {
        meteorView.right = 0;
        meteorView.centerY = arc4random_uniform(self.width*2/3);
    } else {
        meteorView.bottom = -2 * meteorView.height;
        meteorView.centerX = arc4random_uniform(self.width*2/3);
    }
    
    CABasicAnimation *ani = [CABasicAnimation animationWithKeyPath:@"transform"];
    ani.duration = (arc4random_uniform(30) + 20.0) / 20;
    ani.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeTranslation(self.width, self.width, 0)];
    ani.removedOnCompletion = true;
    ani.timingFunction = [CAMediaTimingFunction functionWithControlPoints:1 :0 :1 :1];
    [meteorView.layer addAnimation:ani forKey:@"ani"];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [meteorView removeFromSuperview];
    });
}
- (void)startRain {
    if (_timer) {
        [_timer invalidate];
        _timer = nil;
    }
    _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(rain) userInfo:nil repeats:true];
    
    CAReplicatorLayer *repLayer = (CAReplicatorLayer *)self.layer.sublayers.firstObject;
    CAShapeLayer *itemLayer = (CAShapeLayer *)repLayer.sublayers.firstObject;
    [itemLayer removeAllAnimations];
    [itemLayer addAnimation:[self ani] forKey:@"ani"];
}
- (void)stopRain {
    [_timer invalidate];
    _timer = nil;
    CAReplicatorLayer *repLayer = (CAReplicatorLayer *)self.layer.sublayers.firstObject;
    CAShapeLayer *itemLayer = (CAShapeLayer *)repLayer.sublayers.firstObject;
    [itemLayer removeAllAnimations];
}

@end
